home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- __revision__ = '$Id: resolver.py 503 2005-01-03 21:59:36Z jajcus $'
- __docformat__ = 'restructuredtext en'
- import re
- import socket
- import dns.resolver as dns
- import dns.name as dns
- import dns.exception as dns
- import random
- from encodings import idna
- service_aliases = {
- 'xmpp-server': ('jabber-server', 'jabber') }
- ip_re = re.compile('\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}')
-
- def shuffle_srv(records):
- if not records:
- return []
-
- ret = []
- while len(records) > 1:
- weight_sum = 0
- for rr in records:
- weight_sum += rr.weight + 0.1
-
- thres = random.random() * weight_sum
- weight_sum = 0
- for rr in records:
- weight_sum += rr.weight + 0.1
- if thres < weight_sum:
- records.remove(rr)
- ret.append(rr)
- break
- continue
-
- ret.append(records[0])
- return ret
-
-
- def reorder_srv(records):
- records = list(records)
- records.sort()
- ret = []
- tmp = []
- for rr in records:
- if not tmp or rr.priority == tmp[0].priority:
- tmp.append(rr)
- continue
-
- ret += shuffle_srv(tmp)
-
- if tmp:
- ret += shuffle_srv(tmp)
-
- return ret
-
-
- def resolve_srv(domain, service, proto = 'tcp'):
- names_to_try = [
- u'_%s._%s.%s' % (service, proto, domain)]
- if service_aliases.has_key(service):
- for a in service_aliases[service]:
- names_to_try.append(u'_%s._%s.%s' % (a, proto, domain))
-
-
- for name in names_to_try:
- name = idna.ToASCII(name)
-
- try:
- r = dns.resolver.query(name, 'SRV')
- except dns.exception.DNSException:
- continue
-
- if not r:
- continue
-
- return [ (rr.target.to_text(), rr.port) for rr in reorder_srv(r) ]
-
-
-
- def getaddrinfo(host, port, family = 0, socktype = socket.SOCK_STREAM, proto = 0, allow_cname = True):
- ret = []
- if proto == 0:
- proto = socket.getprotobyname('tcp')
- elif type(proto) != int:
- proto = socket.getprotobyname(proto)
-
- if type(port) != int:
- port = socket.getservbyname(port, proto)
-
- if family not in (0, socket.AF_INET):
- raise NotImplementedError, 'Protocol family other than AF_INET not supported, yet'
-
- if ip_re.match(host):
- return [
- (socket.AF_INET, socktype, proto, host, (host, port))]
-
- host = idna.ToASCII(host)
-
- try:
- r = dns.resolver.query(host, 'A')
- except dns.exception.DNSException:
- r = dns.resolver.query(host + '.', 'A')
-
- if not allow_cname and r.rrset.name != dns.name.from_text(host):
- raise ValueError, 'Unexpected CNAME record found for %s' % (host,)
-
- if r:
- for rr in r:
- ret.append((socket.AF_INET, socktype, proto, r.rrset.name, (rr.to_text(), port)))
-
-
- return ret
-
-